//
//  ZQSubTagViewController.m
//  BuDeJie
//
//  Created by 仲琦 on 16/4/6.
//  Copyright © 2016年 仲琦. All rights reserved.
//

#import "ZQSubTagViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "ZQSubTagItem.h"
#import "ZQSubTagCell.h"
#import "UIColor+Hex.h"
#import <SVProgressHUD/SVProgressHUD.h>

static NSString * const ID = @"subtag";
@interface ZQSubTagViewController ()

@property (nonatomic ,strong)NSArray *subtags;

@property (nonatomic ,weak)NSURLSessionDataTask *task;

@end

@implementation ZQSubTagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"推荐标签";
    
    [self loadData];
    
    //加载xib的cell文件
    [self.tableView registerNib:[UINib nibWithNibName:@"ZQSubTagCell"  bundle:nil]forCellReuseIdentifier:ID];
    
    // 设置分割线占据全屏
//    self.tableView.separatorInset = UIEdgeInsetsZero;
    
    //取消系统分隔线  在cell中将每个cell 的高度 -1
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor = ZQColor(215, 215, 215);
    
    
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    
    //移除弹窗指示器
    [SVProgressHUD dismiss];
    
    //取消请求
    [_task cancel];
    
    
}

- (void)loadData
{
    //创建请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //创建可变字典 存储请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    parameters[@"a"] = @"tag_recommend";
    parameters[@"action"] = @"sub";
    parameters[@"c"] = @"topic";
    
    //加载弹窗
    [SVProgressHUD showWithStatus:@"正在加载"];
    
    
    //发送请求
    _task = [manager GET:ZQRequestURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //移除弹窗指示器
        [SVProgressHUD dismiss];
        //将字典转换成模型
        _subtags = [ZQSubTagItem mj_objectArrayWithKeyValuesArray:responseObject];
        
        [self.tableView reloadData];
        
        //加载成plist文件
        [responseObject writeToFile:@"/Users/Zhong/Desktop/我的代码/08 - 百思不得姐/subtag.plist" atomically:YES];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
        
        //移除弹窗指示器
        [SVProgressHUD dismiss];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.subtags.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZQSubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    
//    cell.layoutMargins = UIEdgeInsetsZero;
    cell.item = self.subtags[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}


@end